#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <ctime>
#include <fstream>
#include <vector>
using namespace std;


#ifndef FUNCTION_H
#define FUNCTION_H

enum  Type { STUDY, WORK, LIFE };
enum  Priority { HIGH, MID, LOW };
enum  showType{PRIORITY,DAY,MONTH};

tm createTm(int year, int month, int day, int hour, int minute, int second);

struct Task {
    int id;
    string name;
    tm startTime;
    Priority priorityLevel;
    Type category;
    tm remindTime;

    Task()
        : id(0), name(""), priorityLevel(Priority::MID), category(Type::WORK) {
        time_t now = time(0);
        startTime = *localtime(&now);
        remindTime = *localtime(&now);
    }

    Task(int id, const string& name, const tm& startTime, Priority priorityLevel = MID, Type category = LIFE, const tm& remindTime = createTm(2000,1,1,0,0,0))
        : id(id), name(name), startTime(startTime), priorityLevel(priorityLevel), category(category), remindTime(remindTime) {

        // 默认提醒时间为开始时间前一小时
        time_t t0=946684800;
        if (difftime(mktime(const_cast<tm*>(&remindTime)),t0)==0){
            time_t start = mktime(const_cast<tm*>(&startTime));
            start -= 3600; // 减去一小时（3600秒）
            this->remindTime = *localtime(&start);
        }
    }

    // 将时间转换为字符串，方便保存到文件和显示
    string timeToString(const tm& timeStruct) const {
        char buffer[80];
        strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &timeStruct);
        return string(buffer);
    }

    // 从字符串解析时间
    tm stringToTime(const string& timeString) const {
        tm timeStruct = {};
        istringstream ss(timeString);
        ss >> get_time(&timeStruct, "%Y-%m-%d %H:%M:%S");
        return timeStruct;
    }

    //将任务保存至文件
    void saveToFile(ofstream& outFile) const {
        outFile << id << " " << name << " " << timeToString(startTime) << " "
                << static_cast<int>(priorityLevel) << " " << static_cast<int>(category) << " "
                << timeToString(remindTime) << "\n";
    }

    
};

static Task loadFromFile(ifstream &inFile);
void show_vectortask(vector<Task> tasks);//输出现在保存在vector中的任务；
void remindTask();
void get_t(int& year, int& month, int& day);
void get_t(int& year, int& month);

#endif
